package com.zondy.mapgis.psmap.manager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zondy.mapgis.common.core.utils.DateUtils;
import com.zondy.mapgis.common.core.utils.StringUtils;
import com.zondy.mapgis.domain.psmap.AppStoreWidget;
import com.zondy.mapgis.domain.system.SysUser;
import com.zondy.mapgis.psmap.manager.mapper.AppStoreWidgetMapper;
import com.zondy.mapgis.psmap.manager.service.IAppStoreWidgetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppStoreWidgetServiceImpl extends ServiceImpl<AppStoreWidgetMapper, AppStoreWidget> implements IAppStoreWidgetService {
   @Autowired
   private AppStoreWidgetMapper appStoreWidgetMapper;

   @Override
   public AppStoreWidget selectAppStoreWidgetByAppWidgetId(String appWidgetId) {
      return this.appStoreWidgetMapper.selectAppStoreWidgetByAppWidgetId(appWidgetId);
   }

   @Override
   public AppStoreWidget selectAppStoreWidgetByWidgetName(String widgetName) {
      return this.appStoreWidgetMapper.selectAppStoreWidgetByWidgetName(widgetName);
   }

   @Override
   public List<AppStoreWidget> selectAppStoreWidgetList(AppStoreWidget appStoreWidget, Long userId) {
      List<AppStoreWidget> appWidgetlist = null;
      if (SysUser.isAdmin(userId)) {
         appWidgetlist = this.appStoreWidgetMapper.selectAppStoreWidgetList(appStoreWidget);
      } else {
         appStoreWidget.getParams().put("userId", userId);
         appWidgetlist = this.appStoreWidgetMapper.selectAppStoreWidgetListByUserId(appStoreWidget);
      }

      return appWidgetlist;
   }

   @Override
   public int insertAppStoreWidget(AppStoreWidget appStoreWidget) {
      appStoreWidget.setCreateTime(DateUtils.getNowDate());
      return this.appStoreWidgetMapper.insertAppStoreWidget(appStoreWidget);
   }

   @Override
   public int updateAppStoreWidget(AppStoreWidget appStoreWidget) {
      if (StringUtils.isEmpty(appStoreWidget.getAppWidgetId()) && StringUtils.isNotNull(appStoreWidget.getWidget()) && StringUtils.isNotEmpty(appStoreWidget.getWidget().getWidgetName())) {
         AppStoreWidget appStoreWidgetByWidgetName = this.appStoreWidgetMapper.selectAppStoreWidgetByWidgetName(appStoreWidget.getWidget().getWidgetName());
         if (StringUtils.isNull(appStoreWidgetByWidgetName)) {
            return 0;
         }

         appStoreWidget.setAppWidgetId(appStoreWidgetByWidgetName.getAppWidgetId());
      }

      appStoreWidget.setUpdateTime(DateUtils.getNowDate());
      return this.appStoreWidgetMapper.updateAppStoreWidget(appStoreWidget);
   }

   @Override
   public int deleteAppStoreWidgetByAppWidgetIds(String[] appWidgetIds) {
      return this.appStoreWidgetMapper.deleteAppStoreWidgetByAppWidgetIds(appWidgetIds);
   }

   @Override
   public int deleteAppStoreWidgetByAppWidgetId(String appWidgetId) {
      return this.appStoreWidgetMapper.deleteAppStoreWidgetByAppWidgetId(appWidgetId);
   }
}
